home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / Peon / PeonSDK-Win32-1.0.0.exe / {app} / PeonMain / source / EngineCore.cpp < prev    next >
C/C++ Source or Header  |  2005-11-30  |  5KB  |  258 lines

  1.  
  2. #include "FileLogger.h"
  3. #include "IniConfigReader.h"
  4. #include "SceneRenderer.h"
  5. #include "IApplication.h"
  6.  
  7. #include "EngineCore.h"
  8.  
  9. namespace peon
  10. {
  11.     //-----------------------------------------------------------------------
  12.     template<> EngineCore* ISingleton<EngineCore>::ms_Singleton = 0;
  13.  
  14.     EngineCore* EngineCore::getSingletonPtr(void)
  15.     {
  16.         return ms_Singleton;
  17.     }
  18.  
  19.     EngineCore& EngineCore::getSingleton(void)
  20.     {  
  21.  
  22.         assert( ms_Singleton );  
  23.  
  24.         return ( *ms_Singleton ); 
  25.     }
  26.  
  27.     EngineCore::EngineCore()
  28.     {
  29.         m_pApplication = NULL;
  30.         m_pConfig      = NULL;
  31.         m_pVideoDevice   = NULL;
  32.         m_fps           = 0.0f;
  33.     }
  34.  
  35.     EngineCore::~EngineCore()
  36.     {
  37.     }
  38.  
  39.     bool EngineCore::loadEngine(const String& strWindowTitle, const String& strIniConfig )
  40.     {
  41.  
  42. #if defined( WIN32 )
  43.         
  44. #if defined(DEBUG) | defined(_DEBUG)
  45.         _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
  46. #endif
  47.  
  48. #endif
  49.  
  50.     
  51.         new FileLogger( PEON_LOG_DEBUG );
  52.  
  53.         if(!FileLogger::getSingleton().openLogStream("PeonMain.log"))
  54.         {
  55.             OutputDebugString("failed to open file\n");
  56.             unloadEngine();
  57.             return false;
  58.         }
  59.  
  60.         //start up SDL
  61.         if(SDL_Init( SDL_INIT_EVERYTHING ) < 0)
  62.         {
  63.             FileLogger::getSingleton().logFatal("EngineCore", "Failed to load SDL. Exiting");
  64.             unloadEngine();
  65.             return false;
  66.         }
  67.  
  68.         m_pConfig = new IniConfigReader( strIniConfig );
  69.         if(NULL == m_pConfig)
  70.         {
  71.             FileLogger::getSingleton().logFatal("EngineCore", "Failed to load INI file. Exiting");
  72.             unloadEngine();
  73.             return false;
  74.         }
  75.  
  76.     
  77.         m_pVideoDevice = new SceneRenderer();
  78.         if(!m_pVideoDevice->loadDevice( m_pConfig ))
  79.         {
  80.             FileLogger::getSingleton().logFatal("EngineCore", "Failed to load graphics renderer. Exiting");
  81.             unloadEngine();
  82.             return false;
  83.         }
  84.  
  85.             
  86.         SDL_WM_SetCaption(strWindowTitle.c_str(), strWindowTitle.c_str());    
  87.  
  88.         //Initialize the AudioEngine instance
  89.         new AudioEngine();
  90.         if(!AudioEngine::getSingleton().loadEngine( m_pConfig ))
  91.         {
  92.             FileLogger::getSingleton().logInfo("EngineCore", "Failed to initialize sound device. Disabling audio support");
  93.         }
  94.  
  95.         new InputEngine();
  96.         if(!InputEngine::getSingleton().loadEngine( m_pConfig ))
  97.         {
  98.  
  99.             FileLogger::getSingleton().logFatal("EngineCore", "Failed to initialize input device(s). Exiting");
  100.             unloadEngine();
  101.             return false;
  102.  
  103.         }
  104.  
  105.     
  106.  
  107.     
  108.         
  109.         return true;
  110.     }
  111.  
  112.     void EngineCore::unloadEngine()
  113.     {
  114.         FileLogger::getSingleton().logDebug("EngineCore", "Shutting down Peon");
  115.  
  116.         if( m_pApplication )
  117.         {
  118.             m_pApplication->onUnloadWorld();
  119.             m_pApplication->unloadStates();
  120.             //m_pApplication->getSingleton().onUnloadWorld();
  121.             //m_pApplication->getSingleton().unloadStates();
  122.  
  123.             //delete IApplication::getSingletonPtr();
  124.             PEON_DELETE( m_pApplication );
  125.         }
  126.  
  127.         
  128.  
  129.     
  130.         //cleanup the AudioEngine instance
  131.         delete AudioEngine::getSingletonPtr();
  132.         
  133.         delete InputEngine::getSingletonPtr();    
  134.         
  135.         PEON_DELETE( m_pVideoDevice );
  136.  
  137.         PEON_DELETE( m_pConfig );    
  138.  
  139.         SDL_Quit();
  140.  
  141.         delete FileLogger::getSingletonPtr();
  142.  
  143.     }
  144.  
  145.     int EngineCore::runEngine()
  146.     {
  147.         m_oTimer.start();
  148.  
  149.         bool done = false;                                     
  150.         SDL_Event event;
  151.  
  152.         while(! done)                         
  153.         {                            
  154.             while( SDL_PollEvent(&event) )                    
  155.             {
  156.                 switch ( event.type )                         
  157.                 {
  158.  
  159.                     case SDL_KEYDOWN:
  160.                     case SDL_KEYUP:
  161.                         //first test for the ESCAPE key
  162.                         if(event.key.keysym.sym == SDLK_ESCAPE)
  163.                         {
  164.                             //quit the app
  165.                             done = true;
  166.                             break;
  167.                         }
  168.  
  169.                         if(m_pApplication) 
  170.                             m_pApplication->onKeyEvent(&event.key);
  171.                     break;
  172.  
  173.                     case SDL_MOUSEMOTION:
  174.                     case SDL_MOUSEBUTTONDOWN:
  175.                         if(m_pApplication)
  176.                             m_pApplication->onMouseEvent( &event );
  177.                     break;
  178.  
  179.                     
  180.  
  181.                 case SDL_QUIT :                                         
  182.                     done = true;                                        
  183.                     break;               
  184.  
  185.                 default:                                    
  186.                     break;                                  
  187.                 } 
  188.             }
  189.  
  190.             float elapsed_time = m_oTimer.getElapsedTime();
  191.  
  192.             if(m_pApplication)
  193.             {
  194.                 m_pApplication->onUpdateWorld( elapsed_time );
  195.                 
  196.             }
  197.  
  198.             if(m_pVideoDevice->clearDevice())
  199.             {
  200.  
  201.                 if(m_pApplication)
  202.                 {
  203.                     m_pApplication->onRenderWorld();
  204.                     
  205.                 }
  206.  
  207.                 
  208.                 m_pVideoDevice->flipDevice();
  209.  
  210.                 updateFPS();
  211.  
  212.  
  213.             }
  214.  
  215.         } 
  216.  
  217.         unloadEngine();
  218.  
  219.         return 0;
  220.     }
  221.  
  222.     bool EngineCore::setApplication( IApplication* pApp )
  223.     {
  224.         if( NULL == pApp )
  225.             return false;
  226.  
  227.         if(!pApp->onLoadWorld())
  228.         {
  229.             FileLogger::getSingleton().logError("EngineCore", "Error on IApplication::onLoad()");
  230.  
  231.             return false;
  232.         }
  233.         
  234.         m_pApplication = pApp;
  235.  
  236.         return true;
  237.     }
  238.  
  239.     void EngineCore::updateFPS()
  240.     {
  241.         static float fLastTime = 0.0f;
  242.         static DWORD dwFrames  = 0L;
  243.         float fTime = m_oTimer.getAbsoluteTime();
  244.         
  245.         ++dwFrames;
  246.  
  247.         // Update the scene stats once per second
  248.         if( fTime - fLastTime > 1.0f )
  249.         {
  250.             m_fps    = dwFrames / (fTime - fLastTime);
  251.             fLastTime = fTime;
  252.             dwFrames  = 0L;
  253.         }
  254.  
  255.     }
  256. }
  257.  
  258.